//
//  XMMeetDetailViewController.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/11.
//

#import "XMMeetDetailViewController.h"
#import "XMMeetDetailView.h"
#import "XMNetwork+home.h"
#import "XMNetwork+mine.h"
#import "XMMeetDetailModel.h"
#import "XMConfirmOrderViewController.h"
#import "XMUserHomePageViewController.h"
//#import <TIMCommon/TIMDefine.h>
//#import <TUICore/TUIThemeManager.h>
//#import "TUILogin.h"
#import "XMMeetBoxViewController.h"
#import "XMChatViewController.h"
#import <WXApi.h>

@interface XMMeetDetailViewController ()<XMMeetDetailViewDelegate>

@property (nonatomic, strong) XMMeetDetailView *detailView;
@property (nonatomic, strong) XMMeetDetailModel *detailModel;

@end

@implementation XMMeetDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    [self.view addSubview:self.detailView];
    [self getDetailInfo];
}

- (void)getDetailInfo {
    XMWeakSelf;
    [_Network getMeetDetailWithServiceId:self.serviceId
                                 success:^(id  _Nonnull responseObject) {
        XMMeetDetailModel *detailModel = [XMMeetDetailModel yy_modelWithJSON:responseObject[@"data"]];
        weakSelf.detailModel = detailModel;
        [weakSelf.detailView setContentWithDetailModel:detailModel];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

#pragma mark - Delegate
//关注
- (void)attentionActionWithAttentionStatus:(NSString *)attentionStatus {
    XMWeakSelf;
    if ([attentionStatus isEqualToString:@"1"]) {
        [_Network cancelAttentionWithUserId:self.detailModel.authorVo.objectId success:^(id  _Nonnull responseObject) {
            [weakSelf getDetailInfo];
        } failure:^(NSError * _Nonnull error) {
            
        }];
    }
    else {
        [_Network friendAttentionWithUserId:self.detailModel.authorVo.objectId
                                    success:^(id  _Nonnull responseObject) {
            [weakSelf getDetailInfo];
        } failure:^(NSError * _Nonnull error) {
            
        }];
    }
}

//聊一聊
- (void)chatAction {
    XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
    XMWeakSelf;
    [_Network getUserSigWithUserId:userModel.userId
                           success:^(id  _Nonnull reponseObject) {
        XMChatViewController *chatVC = [[XMChatViewController alloc] init];
        chatVC.sender = userModel.userId;
        chatVC.senderAvatar = userModel.avatar;
        chatVC.receiver = weakSelf.detailModel.authorVo.objectId;
        chatVC.receiverName = weakSelf.detailModel.authorVo.nickname;
        chatVC.receiverAvatar = weakSelf.detailModel.authorVo.avatar;
        chatVC.userSigStr = [reponseObject safeStringForKey:@"data"];
        [weakSelf.navigationController pushViewController:chatVC animated:YES];
    } failure:^(NSError * _Nonnull error) {
        
    }];
//    [TUILogin login:1600078076
//             userID:userModel.userId
//            userSig:@"8ab0580716920aa7967b8b371c5ced46880159486db96e2c61468c7dbf5e513a"
//               succ:^{
//        [MBProgressHUD showSuccess:@"登录成功"];
//        NSLog(@"登录成功");
//    } fail:^(int code, NSString * _Nullable msg) {
//        [MBProgressHUD showError:@"登录失败"];
//        NSLog(@"登录失败");
//    }];
}

//分享
- (void)shareAction {
    [WXApi registerApp:@"wxe27fd4207b87851f" universalLink:@"https://help.wechat.com/zmy/"];
    
    WXWebpageObject *object = [WXWebpageObject object];
//    object.webpageUrl = [NSString stringWithFormat:@"zmy://%@", self.serviceId];
    object.webpageUrl = [NSString stringWithFormat:@"http://zhimayaner.com/appRedict?serviceId=%@", self.serviceId];
    
    WXMediaMessage *message = [WXMediaMessage message];
    message.title = @"见面包";//[NSString stringWithFormat:@"免费领取%@，就差你这一刀啦，快帮我点点吧！", _cutPriceInfoModel.leftover_price];//@"砍价";
    message.description = @"测试描述";//@"小程序描述";
    message.thumbData = nil;  //兼容旧版本节点的图片，小于32KB，新版本优先
                              //使用WXMiniProgramObject的hdImageData属性
    message.mediaObject = object;
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    req.scene = WXSceneSession;  //目前只支持会话
    
    [WXApi sendReq:req completion:^(BOOL success) {
        
    }];
}

//查看橱窗
- (void)checkUserInfoWithUserId:(NSString *)userId {
    XMUserHomePageViewController *homePageVC = [[XMUserHomePageViewController alloc] init];
    homePageVC.userId = userId;
    [self.navigationController pushViewController:homePageVC animated:YES];
}

//收藏
- (void)collectionAction {
    XMWeakSelf;
    if ([self.detailModel.collectionStatus isEqualToString:@"1"]) {
        [_Network cancelCollectMeetWithServiceId:_serviceId
                                         success:^(id  _Nonnull responseObject) {
            weakSelf.detailModel.collectionStatus = @"0";
            [weakSelf.detailView collectSuccessWithStatus:@"0"];
        } failure:^(NSError * _Nonnull error) {
            
        }];
    }
    else {
        [_Network collectionMeetWithServiceId:_serviceId
                                      success:^(id  _Nonnull responseObject) {
            weakSelf.detailModel.collectionStatus = @"1";
            [weakSelf.detailView collectSuccessWithStatus:@"1"];
        } failure:^(NSError * _Nonnull error) {
            
        }];
    }
}

//添加到见面箱
- (void)addMeetBoxAction {
    [_Network addMeetBoxWithServiceId:_serviceId
                              success:^(id  _Nonnull responseObject) {
        [MBProgressHUD showSuccess:@"添加成功"];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}
//下单
- (void)orderAction {
    XMConfirmOrderViewController *confirmOrderVC = [[XMConfirmOrderViewController alloc] init];
    confirmOrderVC.meetDetailModelArr = @[self.detailModel];
    [self.navigationController pushViewController:confirmOrderVC animated:YES];
}

//见面箱
- (void)meetBoxAction {
    XMMeetBoxViewController *meetBoxVC = [[XMMeetBoxViewController alloc] init];
    [self.navigationController pushViewController:meetBoxVC animated:YES];
}

#pragma mark - Init
- (XMMeetDetailView *)detailView {
    if (!_detailView) {
        _detailView = [[XMMeetDetailView alloc] initWithFrame:self.view.bounds];
        _detailView.delegate = self;
    }
    return _detailView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
